package net.sureon.web.user;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.app.UserSession;
import net.sureon.biz.DailyTimingBO;
import net.sureon.biz.DailyTimingMgr;
import net.sureon.dao.DailyTiming;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * 处理提交考勤 处理防止重复提交考勤 处理编辑考勤
 * 
 * @author Administrator
 * 
 */
public class Apply implements Controller {

	private DailyTimingMgr dailyTimingMgr;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		long userID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getUserID();

		long companyID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getCompanyID();

		long groupID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getGroupID();

		String status = request.getParameter("status");

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = new String(request.getParameter("workDate"));
		java.util.Date workDate = sdf.parse(date);

		DailyTiming dailyTiming = null;

		// 判断是否是编辑更新考勤
		String edit = request.getParameter("edit");
		if ("edit".equals(edit)) {
			// 表示是对已经存存在的考勤的更新
			long dailyTimingID = Long.parseLong(request
					.getParameter("dailyTimingID"));

			dailyTiming = new DailyTiming();
			float workTime = Float.valueOf(new String(request
					.getParameter("workTime")));
			float overTime = Float.valueOf(new String(request
					.getParameter("overTime")));
			float vocationTime = Float.valueOf(new String(request
					.getParameter("vocationTime")));

			float leaveTime = Float.valueOf(new String(request
					.getParameter("leaveTime")));
//			String wd = request.getParameter("workDate");
			// 原先的考勤日期
//			Date sourceWorkDate = sdf.parse(wd);

			String workSummary = request.getParameter("workSummary");

			dailyTimingMgr.editDailyTiming(companyID, dailyTimingID, workTime,
					overTime, vocationTime, leaveTime, workSummary,
					new java.util.Date(), status);

		} else {
			// 进行添加操作

			dailyTiming = dailyTimingMgr.getDailyTimingByWorkDate(companyID,
					userID, workDate);

			if (dailyTiming == null) {
				// 表示该用户在今天并没有考勤，允许其提交

				// if(workDate.getTime() > sdf.parse(sdf.format(new
				// java.util.Date())).getTime()) {
				// 不允许对未来的考勤,添加失败！

				// } else {

				dailyTiming = new DailyTiming();
				float workTime = Float.valueOf(new String(request
						.getParameter("workTime")));
				float overTime = Float.valueOf(new String(request
						.getParameter("overTime")));
				float vocationTime = Float.valueOf(new String(request
						.getParameter("vocationTime")));

				float leaveTime = Float.valueOf(new String(request
						.getParameter("leaveTime")));

				Date submitDate = new Date();

				String workSummary = request.getParameter("workSummary");

				dailyTiming.setCompanyID(companyID);
				dailyTiming.setGroupID(groupID);
				dailyTiming.setUserID(userID);
				dailyTiming.setWorkDate(workDate);
				dailyTiming.setWorkTime(workTime);
				dailyTiming.setOverTime(overTime);
				dailyTiming.setVocationTime(vocationTime);
				dailyTiming.setLeaveTime(leaveTime);
				dailyTiming.setWorkSummary(workSummary);
				dailyTiming.setSubmitDate(submitDate);
				dailyTiming.setStatus(status);

				dailyTimingMgr
						.insertDailyTiming(new DailyTimingBO(dailyTiming));
				// }
			} else {
				// 表示该用户今天已经考勤过了，不允许其提交
				response
						.sendRedirect(request.getContextPath()
								+ "/user/myDailyTiming.do?errorMsg=already_submitted");
				return null;

			}
		}
		response.sendRedirect(request.getContextPath()
				+ "/user/myDailyTiming.do");
		return null;
	}

	public DailyTimingMgr getDailyTimingMgr() {
		return dailyTimingMgr;
	}

	public void setDailyTimingMgr(DailyTimingMgr dailyTimingMgr) {
		this.dailyTimingMgr = dailyTimingMgr;
	}
}
